package com.itheima.reggie.controller;
import cn.hutool.core.collection.CollectionUtil;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import com.sun.xml.bind.v2.model.core.ID;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;



@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/list")
    public ResultInfo fingByCategrotId(Long categoryId) {
        String key="dish_"+categoryId;

        List<Dish> byCategoryId =(List<Dish>) redisTemplate.opsForValue().get(key);

        if (CollectionUtil.isNotEmpty(byCategoryId)){
            log.info("从redis缓存中取数据");

            return ResultInfo.success(byCategoryId);
        }

         byCategoryId = dishService.findByCategoryId(categoryId);

        redisTemplate.opsForValue().set(key,byCategoryId);

        return ResultInfo.success(byCategoryId);
    }

}
